program define ivclp_bininstr, eclass
syntax varlist, ENdog(string) [FGM] [IFGM] [FM] [EXTra(string)] [noVHAT] [MATrix(name)]
// Parse Varlist
tokenize `varlist'
	local dep `1'
	macro shift
	local control `*'
// Parse Endogenous/Instrument Variables
tokenize `"`endog'"', parse(" =")
	local endog `1'
	macro shift 2
	local instr `*'
tokenize `"`extra'"'
	local extvar `*'
tokenize `"`name'"'
	local init `*'
if  `"`name'"' != ""{
global from , from(`init')
}
else{
    global from 
}
	
	tempname vtild v1pdf v0pdf v1cdf v0cdf vcdf v mua mub sda sdb p vpdf
	quietly reg `endog' `instr' `control'
	quietly predict `vtild', resid
	
	if `"`fgm'"' == "fgm"{
		sort `vtild'
		quietly kdensity `vtild' if `instr'==1, at(`vtild') gen(`v1pdf') nograph
		quietly gen `v1cdf' = 0 if _n ==1 
		quietly replace `v1cdf' = `v1pdf'*(`vtild' - `vtild'[_n-1]) + `v1cdf'[_n-1] if _n>1
		quietly kdensity `vtild' if `instr'==0, at(`vtild') gen(`v0pdf') nograph
		quietly gen `v0cdf' = 0 if _n ==1 
		quietly replace `v0cdf'= `v0pdf'*(`vtild' - `vtild'[_n-1]) + `v0cdf'[_n-1] if _n>1
		quietly gen `vcdf' = `v0cdf'*(1-`instr') + `v1cdf'*`instr'
		
		quietly gen v = `vcdf' - 1/2
		display "CLP with F(vtilde|Z)-1/2 as V"
		quietly reg  `dep' c.`endog' `control' c.v c.v#c.(`instr' `control') c.`endog'#c.v c.`endog'#c.v#c.(`instr' `control')  `extvar'
	}
	else if `"`ifgm'"' == "ifgm"{
		sort `vtild'
		quietly kdensity `vtild' if `instr'==1, at(`vtild') gen(`v1pdf') nograph
		quietly gen `v1cdf' = 0 if _n ==1 
		quietly replace `v1cdf' = `v1pdf'*(`vtild' - `vtild'[_n-1]) + `v1cdf'[_n-1] if _n>1
		quietly kdensity `vtild' if `instr'==0, at(`vtild') gen(`v0pdf') nograph
		quietly gen `v0cdf' = 0 if _n ==1 
		quietly replace `v0cdf'= `v0pdf'*(`vtild' - `vtild'[_n-1]) + `v0cdf'[_n-1] if _n>1
		quietly gen `vcdf' = `v0cdf'*(1-`instr') + `v1cdf'*`instr'
		
		quietly gen v = `vcdf' - 1/2
		quietly gen v2 = `vcdf'*(2-3*`vcdf')
		local vlist v v2
		display "CLP with F(vtilde|Z)-1/2 and F(vtilde|Z)(2-3F(vtilde|Z)) as V"
		quietly reg  `dep' c.`endog' `control' c.(`vlist') c.(`vlist')#c.(`instr' `control') c.`endog'#c.(`vlist') c.`endog'#c.(`vlist')#c.(`instr' `control') `extvar'
	
	
	}
	else if `"`fm'"' == "fm"{
		capture {
		    quietly fmm 2$from : regress `vtild' `instr'
		}
		if _rc==0{
		quietly gen `mua' = _b[`vtild':1.Class] +_b[`vtild':1.Class#c.`instr']*`instr'
		quietly gen `mub' = _b[`vtild':2.Class] +_b[`vtild':2.Class#c.`instr']*`instr'
		scalar `sda' = _b[/var(e.`vtild')#1.Class]^.5 
		scalar `sdb' = _b[/var(e.`vtild')#2.Class]^.5
		quietly estat lcprob, nose post
		scalar `p' = _b[1.Class]
		quietly gen `vpdf' = `p'*normalden((`vtild' - `mua'),`sda' ) + (1-`p')*normalden((`vtild' - `mub'),`sdb' ) 
		quietly gen v= (normalden((`vtild' - `mua'),`sda' )- normalden((`vtild' - `mub'),`sdb' ))/`vpdf'
		quietly gen v2 = (normalden((`vtild' - `mua'),`sda' )/`vpdf')*(`vtild'-`mua')/`sda'^2 
		quietly gen v3 = (normalden((`vtild' - `mub'),`sdb' )/`vpdf')*(`vtild'-`mub')/`sdb'^2 
		local vlist v2 v3
		display "CLP assuming Finite (2) Mixture of Normal"
		quietly reg  `dep' c.`endog' `control' c.v c.v#c.`endog' c.(`vlist') c.(`vlist')#c.(`instr' `control') c.`endog'#c.(`vlist') c.`endog'#c.(`vlist')#c.(`instr' `control')  `extvar'
		}
		else{
		display "Finite Mixture did not Converge"
		ereturn clear
	}
	}
	else{    
		gen v = `vtild'
		display "CLP with vtilde as V"
		quietly reg  `dep' c.`endog' `control' c.v c.v#c.(`instr' `control') c.`endog'#c.v c.`endog'#c.v#c.(`instr' `control')  `extvar'
	}	
	ereturn display
	if `"`vhat'"' == "novhat"{
	quietly drop v*
	}
end